package cn.iocoder.yudao.module.system.controller.admin.merchant;


import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.system.controller.admin.merchant.vo.AdminGetMerchantRespVO;
import cn.iocoder.yudao.module.system.controller.admin.merchant.vo.StoreProfileReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.merchant.MerchantDO;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import cn.iocoder.yudao.module.system.service.user.AdminUserService;
import cn.iocoder.yudao.module.system.service.merchant.MerchantService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "商家后台-店铺管理")
@RestController
@RequestMapping("/system/admin-merchant")
@Validated
public class StoreManagementController {

    @Resource
    private AdminUserService userService;
    @Resource
    private MerchantService merchantService;

    @GetMapping("/getStore")
    @Operation(summary = "获取店铺信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<AdminGetMerchantRespVO> getStoreProfile(){
        AdminUserDO user = userService.getUser(getLoginUserId());
        CommonResult<MerchantDO> storeProfile= merchantService.getStoreProfile(user.getId());
        MerchantDO merchantDO = storeProfile.getData();
        AdminGetMerchantRespVO respVO = new AdminGetMerchantRespVO();
        respVO.setMerchantName(merchantDO.getMerchantName());
        respVO.setMainProduct(merchantDO.getMainProduct());
        respVO.setDescription(merchantDO.getDescription());
        respVO.setContactAddress(merchantDO.getContactAddress());

        // 返回成功响应
        return CommonResult.success(respVO);
    }
    @Operation(summary = "修改店铺信息")
    @PutMapping("/updateStore")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<Boolean> updateStore(@Valid @RequestBody StoreProfileReqVO reqVO) {
        AdminUserDO user = userService.getUser(getLoginUserId());
        reqVO.setMerchantId(user.getId());
        merchantService.updateStoreprofile(reqVO);
        return success(true);
    }

}
